package com.example.design.clean_craftsmanship

import android.app.AlertDialog
import android.content.Context
import android.widget.Toast


/**
 *Create by Fanketly on 2022/9/3
 *Gitee:https://gitee.com/fanketly
 *desc:
 */


interface Authenticator {
    fun authenticate(username: String, password: String): Boolean?
}

/**
 *模拟需要传入Db操作类
 **/
class RealAuthenticator(private val db: String) :
    com.example.design.clean_craftsmanship.Authenticator {

    override fun authenticate(username: String, password: String): Boolean {
        return username == db && password == db
    }
}

/**
 *Dummy是一种什么都不做的实现方式，方法有返回值时尽量返回null或0
 **/
class AuthenticatorDummy : com.example.design.clean_craftsmanship.Authenticator {

    override fun authenticate(username: String, password: String): Boolean? {
        return null
    }

}

/**
 *Stub 返回测试所需的特定值
 **/
class RejectingAuthenticator : com.example.design.clean_craftsmanship.Authenticator {

    override fun authenticate(username: String, password: String): Boolean {
        return false
    }

}

/**
 *Stub 返回测试所需的特定值
 **/
class PromiscuousAuthenticator : com.example.design.clean_craftsmanship.Authenticator {

    override fun authenticate(username: String, password: String): Boolean {
        return true
    }

}

/**
 *spy返回测试所需的特定值，推动系统沿着我们期望的路径前行，并能记住它所做的事
 **/
open class SpyAuthenticator : com.example.design.clean_craftsmanship.Authenticator {
    var lastPassword = ""
    var lastUsername = ""
    var result = false
    var count = 0

    override fun authenticate(username: String, password: String): Boolean {
        count++
        this.lastPassword = password
        this.lastUsername = username
        return result
    }

}

/**
 *Mock是一种spy，它还知道我们的预期，并判断参数是否通过
 **/
class MockAuthenticator(
    var expectUsername: String,
    var expectPassword: String,
    var expectCount: Int
) : com.example.design.clean_craftsmanship.SpyAuthenticator() {
    fun validate(): Boolean {
        return count == expectCount && lastPassword == expectPassword && lastUsername == expectUsername
    }

}

/**
 *Fake是一种模拟器
 **/
class FakeAuthenticator : com.example.design.clean_craftsmanship.Authenticator {
    override fun authenticate(username: String, password: String): Boolean {
        return username == "user" && password == "good"
    }
}

enum class LoginEvent {
    CLOSE
}

class LoginDialog(private val authenticator: com.example.design.clean_craftsmanship.Authenticator) {

    fun sendEvent(loginEvent: com.example.design.clean_craftsmanship.LoginEvent) {
        when (loginEvent) {
            com.example.design.clean_craftsmanship.LoginEvent.CLOSE -> println("Close")
        }
    }

    fun submit(username: String, password: String): Boolean {
        return authenticator.authenticate(username, password) ?: false
    }

    fun newDialog(context: Context) {
        AlertDialog.Builder(context).setTitle("登录").setMessage("登录确认")
            .setNegativeButton("权限") { dialog, _ ->
                dialog.dismiss()
            }.setPositiveButton("确认") { dialog, _ ->
                Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show()
                dialog.dismiss()
            }.create().show()
    }
}